<?php
/*
* Copyright (c) 2010 by Cediti
*
* You may distribute under the terms of either the GNU Lesser General
* Public License or the new BSD License, as specified in the README file.
*/
// created :    09/02/2010
// version :    02/04/2010

class Cmis_Client_Services_Generic_Navigation extends Cmis_Services_Abstract implements Cmis_Services_Navigation
{
	public function getChildren(Cmis_Repository_Id $repositoryId, Cmis_Repository_Id $folderId, $maxItems = null, $skipCount = null, $orderBy = null, $filter = null, $includeRelationships = null, $renditionFilter = null, $includeAllowableActions = false, $includePathSegment = false)
	{
        if ($maxItems < 0)
        {
            $maxItems = 0;
        }

        if ($skipCount < 0)
        {
            $skipCount = 0;
        }

		$response = $this->getServices()->getProtocolService('Navigation')->getChildren($repositoryId, $folderId, $maxItems, $skipCount, $orderBy, $filter, $includeRelationships, $renditionFilter, $includeAllowableActions, $includePathSegment);
		if (empty($response))
		{
			return null;
		}
		$xml = $response->toXml();

		$ret = array(new Cmis_Repository_Entry($xml, $repositoryId, true));

		$entries = (in_array('atom', $xml->getDocNamespaces()))?$xml->xpath('//atom:entry'):$xml->xpath('//entry');

		//entries
		foreach($entries as $entry)
		{
			$ret[] = new Cmis_Repository_Entry($entry, $repositoryId);
		}

		return $ret;
	}

	public function getDescendants(Cmis_Repository_Id $repositoryId, Cmis_Repository_Id $folderId, $depth = null, $filter = null, $includeRelationships = null, $renditionFilter = null, $includeAllowableActions = false, $includePathSegment = false)
	{
		//getCapapbilities('GetDescendants');
		die('-[Must be implemented : ' . __METHOD__ . ' (' . __FILE__ . '#' . __LINE__ . ')]-');
	}

	public function getFolderTree(Cmis_Repository_Id $repositoryId, Cmis_Repository_Id $folderId, $depth = null, $filter = null, $includeRelationships = null, $renditionFilter = null, $includeAllowableActions = false, $includePathSegment = false)
	{
		$cur = $this->_getRepositoryInfo($repositoryId);

		if (!$cur->getCapabilities('GetFolderTree'))
		{
			throw new Cmis_Exception_Services_NoCapability('Your repository diden\'t have the capability GetFolderTree'); 
		}

		/* @var $response Cmis_Http_Response */ 
		if ($response = $this->getServices()->getProtocolService('Navigation')->getFolderTree($repositoryId, $folderId, $depth, $filter, $includeRelationships, $renditionFilter, $includeAllowableActions, $includePathSegment))
		{
			$xml = $response->toXml();

			//we just need id and title
            $ret = array();
            //$titles = $xml->xpath('//entry/title');
            $ids = $xml->xpath('//entry/cmisra:object/cmis:properties/cmis:propertyId[@propertyDefinitionId="cmis:objectId"]/cmis:value');
            $paths = $xml->xpath('//entry/cmisra:object/cmis:properties/cmis:propertyString[@propertyDefinitionId="cmis:path"]/cmis:value');
            $i = 0;
            //foreach($titles as $node)
            foreach($paths as $node)
            {
            	$id = (string)$ids[ $i ][0];
            	//$path = (string)$paths[ $i ][0];
            	$path = (string)$node[0];
            	//$title = (string)$node[0]
                //$ret[$id] = array($title, $id, $path);
                $ret[ $path ] = array($path, $id);
                $i++;
            }
            ksort($ret);
            return $ret;
		}

		return null;
	}

	public function getFolderParent(Cmis_Repository_Id $repositoryId, Cmis_Repository_Id $folderId, $filter = null)
	{
        if ($response = $this->getServices()->getProtocolService('Navigation')->getFolderParent($repositoryId, $folderId, $filter))
        {
            return new Cmis_Repository_Entry($response->toXml(), $repositoryId);
        }

        return null;
	}

	public function getObjectParents(Cmis_Repository_Id $repositoryId, Cmis_Repository_Id $objectId, $filter = null, $includeRelationships = null, $renditionFilter = null, $includeAllowableActions = false, $includeRelativePathSegment = false)
	{
		if ($response = $this->getServices()->getProtocolService('Navigation')->getObjectParents($repositoryId, $objectId, $filter, $includeRelationships, $renditionFilter, $includeAllowableActions, $includeRelativePathSegment))
		{
			return new Cmis_Repository_Entry($response->toXml()->entry, $repositoryId);
		}

		return null;
	}

	public function getCheckedoutDocuments(Cmis_Repository_Id $repositoryId, Cmis_Repository_Id $folderId = null, $maxItems = null, $skipCount = null, $orderBy = null, $filter = null, $includeRelationships = null, $renditionFilter = null, $includeAllowableActions = false)
	{
		die('-[Must be implemented : ' . __METHOD__ . ' (' . __FILE__ . '#' . __LINE__ . ')]-');
	}
}


# EOF
